package com.zzuli.practice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzuli.practice.entity.Post;
import com.zzuli.practice.entity.Result;
import com.zzuli.practice.entity.User;
import com.zzuli.practice.service.impl.LoginService;
import com.zzuli.practice.service.impl.PostService;
import com.zzuli.practice.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@CrossOrigin
public class PostController implements Constant {

    @Autowired
    private PostService postService;

    @Autowired
    private LoginService loginService;

    @RequestMapping({"/post/{pageIndex}", "/post"})
    public Result getPagePost(@PathVariable(value = "pageIndex", required = false) Integer pageIndex){
        Result result = new Result();
        try{
            IPage<Post> iPage = postService.pagePostList(pageIndex);
            if (iPage != null && iPage.getSize() > 0){
                result.setStatusCode(SUCCESS);
                result.setMessage("success");
                result.setData(iPage);
            } else {
                result.setStatusCode(UNFIND);
                result.setMessage("没有数据");
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping(value = "/insertpost", method = RequestMethod.POST)
    public Result insertPost(String username, String title, String content){
        Result result = new Result();
        // 判断数据合法性
        if (StringUtils.isBlank(username)){
            result.setStatusCode(FAILURE);
            result.setMessage("未登录");
            result.setData(null);
            return result;
        }
        if (StringUtils.isBlank(title)){
            result.setStatusCode(FAILURE);
            result.setMessage("标题不能为空");
            result.setData(null);
            return result;
        }
        if (StringUtils.isBlank(content)){
            result.setStatusCode(FAILURE);
            result.setMessage("内容不能为空");
            result.setData(null);
            return result;
        }

        User user = loginService.selectByUserName(username);
        Post post = new Post();
        post.setTitle(title);
        post.setUsername(username);
        post.setContent(content);
        post.setCreatetime(new Date());
        post.setHeaderimg(user.getHeaderimg());
        int i = postService.insertPost(post);
        if (i == 1){
            result.setStatusCode(SUCCESS);
            result.setMessage("添加成功");
            result.setData(null);
            return result;
        } else {
            result.setStatusCode(FAILURE);
            result.setMessage("添加失败");
            result.setData(null);
            return result;
        }
    }

    @RequestMapping("/getpost/{id}")
    public Result getPost(@PathVariable("id") Integer id){
        Result result = new Result();
        try{
            Post post = postService.selectPostById(id);
            result.setStatusCode(SUCCESS);
            result.setMessage("查询成功");
            result.setData(post);
        } catch (Exception e){
            e.printStackTrace();
            result.setStatusCode(FAILURE);
            result.setMessage("查询失败");
        }
        return result;
    }

    @RequestMapping(value = {"/searchpost/{pageIndex}", "/searchpost"}, method = RequestMethod.POST)
    public Result searchPost(@PathVariable(value = "pageIndex", required = false) Integer pageIndex,
                             String title, String username){
        Result result = new Result();
        IPage<Post> postIPage = postService.searchPagePost(pageIndex, title, username);
        result.setStatusCode(SUCCESS);
        result.setMessage("查询成功");
        result.setData(postIPage);
        return result;

    }

    @RequestMapping(value = "/deletepost", method = RequestMethod.POST)
    public Result deletePost(Integer id, String username){
        Result result = new Result();

        // 判断数据合法性
        if (StringUtils.isBlank(username)){
            result.setStatusCode(FAILURE);
            result.setMessage("您还没有登录");
            return result;
        }
        if (id == null){
            result.setStatusCode(FAILURE);
            result.setMessage("数据异常");
            return result;
        }

        // 权限判断
        User user = loginService.selectByUserName(username);
        if (user == null){
            result.setStatusCode(FAILURE);
            result.setMessage("用户不存在");
            return result;
        }
        // 查询帖子作者名, 只有帖子作者和管理员可以删除
        Post post = postService.selectPostById(id);

        if (user.getType() == 0 && !username.equals(post.getUsername())){
            result.setStatusCode(FAILURE);
            result.setMessage("您没有权限");
            return result;
        }

        // 删除帖子
        int i = postService.deletePost(id);

        if (i == 1){
            result.setStatusCode(SUCCESS);
            result.setMessage("删除成功");
            return result;
        } else {
            result.setStatusCode(FAILURE);
            result.setMessage("删除失败");
            return result;
        }

    }

}
